#ifndef GAME_DATA_ARRAY_H_
#define GAME_DATA_ARRAY_H_

#include <string>
#include <vector>
#include "luaplus/luaplus.h"
#include "scripthelper.h"
#include "util/log.h"		    // logging


namespace script 
{

template <typename tp_DataStruct> class GameDataArray
{
public:

	//constructor
	GameDataArray()
	{};

	//destructor
	~GameDataArray()
	{};

	//main methods
	bool LoadData(const std::string& File, const std::string& RootObjectName = "GameData")
	{
		//m_Array.clear();

		//1. Loading file
		LuaPlus::LuaState*	pScript = ScriptHelper::CreateLuaState();
		if (!ScriptHelper::DoFile(*pScript, File))
		{
			LuaState::Destroy( pScript );
			return false;
		}
		else
		{

			////2. Processing data
			LuaPlus::LuaObject	RootObject = ScriptHelper::GetObj(*pScript, RootObjectName);
			
			bool GotSomeData = false;

			if (RootObject.IsTable())
			{
				if (RootObject.GetTableCount() > 0)
				{
					
					m_Array.resize(RootObject.GetTableCount());

					for (int i=0; i<RootObject.GetTableCount(); i++)
					{
						LuaPlus::LuaObject	CurrentElement = RootObject.GetByIndex(i+1);
						if (CurrentElement.IsTable())
						{
							GotSomeData = true;
							m_Array.at(i).LoadData(CurrentElement);
						}
					}
				}
			}

		}
		//3. destroying state
		LuaState::Destroy( pScript );

		return true;

	};
	
	int GetElementsCount() {return (int)m_Array.size();};
	bool IsEmpty()		   {return m_Array.empty();};

	tp_DataStruct* GetElement(int Index)
	{
		if (Index < 0 || Index >= (int)m_Array.size())
		{
			return NULL;
		}
		
		return &(m_Array.at(Index));
	};

protected:

	std::vector<tp_DataStruct> m_Array;



};

}
#endif